package com.stu.stusystem.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: cxt
 * @time: 2021/7/27
 * <p>
 * 线程池
 */
@Configuration
@Slf4j
public class TaskExecutorConfig {

    @Bean
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);       // 核心线程池，正常情况下开启数量
        taskExecutor.setMaxPoolSize(15);        // 最大线程数
        taskExecutor.setQueueCapacity(20);      // 等待队列大小
        taskExecutor.setKeepAliveSeconds(60);   // 超时回收
        taskExecutor.setThreadNamePrefix("MyAsync-");// 线程前缀
        taskExecutor.setRejectedExecutionHandler((Runnable r, ThreadPoolExecutor exe) -> {
            log.warn("MyThreadPool-当前任务线程池队列已满!");
        });//配置拒绝策略
        taskExecutor.initialize();
        return taskExecutor;
    }
}
